<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/saiki.jpeg?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/saiki.jpeg?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Java,mina,">










<meta name="description" content="从服务端的角度来看,首先是1234567 /**     * MINA NIO线程数量     */public static int MINA_PROCESSOR = Runtime.getRuntime().availableProcessors() + 1;NioSocketAcceptor  acceptor = new NioSocketAcceptor(NetConfig.MINA_P">
<meta name="keywords" content="Java,mina">
<meta property="og:type" content="article">
<meta property="og:title" content="mina连接建立的过程">
<meta property="og:url" content="http://yoursite.com/2020/06/14/mina连接建立的过程/index.html">
<meta property="og:site_name" content="Penn">
<meta property="og:description" content="从服务端的角度来看,首先是1234567 /**     * MINA NIO线程数量     */public static int MINA_PROCESSOR = Runtime.getRuntime().availableProcessors() + 1;NioSocketAcceptor  acceptor = new NioSocketAcceptor(NetConfig.MINA_P">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://note.youdao.com/yws/res/3166/746FEDFCF7FE440CB3D95999DEF0E461">
<meta property="og:updated_time" content="2020-06-14T08:37:40.412Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="mina连接建立的过程">
<meta name="twitter:description" content="从服务端的角度来看,首先是1234567 /**     * MINA NIO线程数量     */public static int MINA_PROCESSOR = Runtime.getRuntime().availableProcessors() + 1;NioSocketAcceptor  acceptor = new NioSocketAcceptor(NetConfig.MINA_P">
<meta name="twitter:image" content="http://note.youdao.com/yws/res/3166/746FEDFCF7FE440CB3D95999DEF0E461">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/06/14/mina连接建立的过程/">





  <title>mina连接建立的过程 | Penn</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Penn</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">my blog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br>
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br>
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/06/14/mina连接建立的过程/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Shen">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/saiki.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Penn">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">mina连接建立的过程</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-06-14T16:37:40+08:00">
                2020-06-14
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/技术/" itemprop="url" rel="index">
                    <span itemprop="name">技术</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2020/06/14/mina连接建立的过程/" class="leancloud_visitors" data-flag-title="mina连接建立的过程">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>从服务端的角度来看,首先是<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> /**</span><br><span class="line">     * MINA NIO线程数量</span><br><span class="line">     */</span><br><span class="line">public static int MINA_PROCESSOR = Runtime.getRuntime().availableProcessors() + 1;</span><br><span class="line"></span><br><span class="line">NioSocketAcceptor  acceptor = new NioSocketAcceptor(NetConfig.MINA_PROCESSOR);</span><br></pre></td></tr></table></figure></p>
<p>进入构造方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class&lt;? extends IoProcessor&lt;S&gt;&gt; processorClass,</span><br><span class="line">           int processorCount) &#123;</span><br><span class="line">       this(sessionConfig, null, new SimpleIoProcessorPool&lt;S&gt;(processorClass, processorCount), true, null);</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<p>会根据传进来的线程数创建一个<code>SimpleIoProcessorPool</code>.<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">    * Creates a new instance of SimpleIoProcessorPool with an executor</span><br><span class="line">    *</span><br><span class="line">    * @param processorType The type of IoProcessor to use</span><br><span class="line">    * @param executor The &#123;@link Executor&#125;</span><br><span class="line">    * @param size The number of IoProcessor in the pool</span><br><span class="line">    * @param selectorProvider The SelectorProvider to used</span><br><span class="line">    */</span><br><span class="line">   @SuppressWarnings(&quot;unchecked&quot;)</span><br><span class="line">   public SimpleIoProcessorPool(Class&lt;? extends IoProcessor&lt;S&gt;&gt; processorType, Executor executor, int size, </span><br><span class="line">           SelectorProvider selectorProvider) &#123;</span><br><span class="line">       if (processorType == null) &#123;</span><br><span class="line">           throw new IllegalArgumentException(&quot;processorType&quot;);</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       if (size &lt;= 0) &#123;</span><br><span class="line">           throw new IllegalArgumentException(&quot;size: &quot; + size + &quot; (expected: positive integer)&quot;);</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       // Create the executor if none is provided</span><br><span class="line">       createdExecutor = executor == null;</span><br><span class="line"></span><br><span class="line">       if (createdExecutor) &#123;</span><br><span class="line">           this.executor = Executors.newCachedThreadPool();</span><br><span class="line">           // Set a default reject handler</span><br><span class="line">           ((ThreadPoolExecutor) this.executor).setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());</span><br><span class="line">       &#125; else &#123;</span><br><span class="line">           this.executor = executor;</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       pool = new IoProcessor[size];</span><br><span class="line"></span><br><span class="line">       boolean success = false;</span><br><span class="line">       Constructor&lt;? extends IoProcessor&lt;S&gt;&gt; processorConstructor = null;</span><br><span class="line">       boolean usesExecutorArg = true;</span><br><span class="line"></span><br><span class="line">       try &#123;</span><br><span class="line">           // We create at least one processor</span><br><span class="line">           try &#123;</span><br><span class="line">               try &#123;</span><br><span class="line">                   processorConstructor = processorType.getConstructor(ExecutorService.class);</span><br><span class="line">                   pool[0] = processorConstructor.newInstance(this.executor);</span><br><span class="line">               &#125; catch (NoSuchMethodException e1) &#123;</span><br><span class="line">                   // To the next step...</span><br><span class="line">                   try &#123;</span><br><span class="line">                       if(selectorProvider==null) &#123;</span><br><span class="line">                           processorConstructor = processorType.getConstructor(Executor.class);</span><br><span class="line">                           pool[0] = processorConstructor.newInstance(this.executor);</span><br><span class="line">                       &#125; else &#123;</span><br><span class="line">                           processorConstructor = processorType.getConstructor(Executor.class, SelectorProvider.class);</span><br><span class="line">                           pool[0] = processorConstructor.newInstance(this.executor,selectorProvider);</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125; catch (NoSuchMethodException e2) &#123;</span><br><span class="line">                       // To the next step...</span><br><span class="line">                       try &#123;</span><br><span class="line">                           processorConstructor = processorType.getConstructor();</span><br><span class="line">                           usesExecutorArg = false;</span><br><span class="line">                           pool[0] = processorConstructor.newInstance();</span><br><span class="line">                       &#125; catch (NoSuchMethodException e3) &#123;</span><br><span class="line">                           // To the next step...</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;</span><br><span class="line">           &#125; catch (RuntimeException re) &#123;</span><br><span class="line">               LOGGER.error(&quot;Cannot create an IoProcessor :&#123;&#125;&quot;, re.getMessage());</span><br><span class="line">               throw re;</span><br><span class="line">           &#125; catch (Exception e) &#123;</span><br><span class="line">               String msg = &quot;Failed to create a new instance of &quot; + processorType.getName() + &quot;:&quot; + e.getMessage();</span><br><span class="line">               LOGGER.error(msg, e);</span><br><span class="line">               throw new RuntimeIoException(msg, e);</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           if (processorConstructor == null) &#123;</span><br><span class="line">               // Raise an exception if no proper constructor is found.</span><br><span class="line">               String msg = String.valueOf(processorType) + &quot; must have a public constructor with one &quot;</span><br><span class="line">                       + ExecutorService.class.getSimpleName() + &quot; parameter, a public constructor with one &quot;</span><br><span class="line">                       + Executor.class.getSimpleName() + &quot; parameter or a public default constructor.&quot;;</span><br><span class="line">               LOGGER.error(msg);</span><br><span class="line">               throw new IllegalArgumentException(msg);</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           // Constructor found now use it for all subsequent instantiations</span><br><span class="line">           for (int i = 1; i &lt; pool.length; i++) &#123;</span><br><span class="line">               try &#123;</span><br><span class="line">                   if (usesExecutorArg) &#123;</span><br><span class="line">                       if(selectorProvider==null) &#123;</span><br><span class="line">                           pool[i] = processorConstructor.newInstance(this.executor);</span><br><span class="line">                       &#125; else &#123;</span><br><span class="line">                           pool[i] = processorConstructor.newInstance(this.executor, selectorProvider);</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125; else &#123;</span><br><span class="line">                       pool[i] = processorConstructor.newInstance();</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125; catch (Exception e) &#123;</span><br><span class="line">                   // Won&apos;t happen because it has been done previously</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           success = true;</span><br><span class="line">       &#125; finally &#123;</span><br><span class="line">           if (!success) &#123;</span><br><span class="line">               dispose();</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<p>可以看到<code>pool = new IoProcessor[size];</code>创建了一个size大小的pool,让后赋值<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">if(selectorProvider==null) &#123;</span><br><span class="line">                            pool[i] = processorConstructor.newInstance(this.executor);</span><br><span class="line">                        &#125; else &#123;</span><br><span class="line">                            pool[i] = processorConstructor.newInstance(this.executor, selectorProvider);</span><br><span class="line">                        &#125;</span><br></pre></td></tr></table></figure></p>
<p>之后就可已跟着&lt;&lt;Mina中的观察者模式&gt;&gt;中的bind方法深入下去,到启动一个Acceptor线程<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line">@Override</span><br><span class="line">      public void run() &#123;</span><br><span class="line">          assert acceptorRef.get() == this;</span><br><span class="line"></span><br><span class="line">          int nHandles = 0;</span><br><span class="line"></span><br><span class="line">          // Release the lock</span><br><span class="line">          lock.release();</span><br><span class="line"></span><br><span class="line">          while (selectable) &#123;</span><br><span class="line">              try &#123;</span><br><span class="line">                  // Process the bound sockets to this acceptor.</span><br><span class="line">                  // this actually sets the selector to OP_ACCEPT,</span><br><span class="line">                  // and binds to the port on which this class will</span><br><span class="line">                  // listen on. We do that before the select because </span><br><span class="line">                  // the registerQueue containing the new handler is</span><br><span class="line">                  // already updated at this point.</span><br><span class="line">                  nHandles += registerHandles();</span><br><span class="line"></span><br><span class="line">                  // Detect if we have some keys ready to be processed</span><br><span class="line">                  // The select() will be woke up if some new connection</span><br><span class="line">                  // have occurred, or if the selector has been explicitly</span><br><span class="line">                  // woke up</span><br><span class="line">                  int selected = select();</span><br><span class="line"></span><br><span class="line">                  // Now, if the number of registered handles is 0, we can</span><br><span class="line">                  // quit the loop: we don&apos;t have any socket listening</span><br><span class="line">                  // for incoming connection.</span><br><span class="line">                  if (nHandles == 0) &#123;</span><br><span class="line">                      acceptorRef.set(null);</span><br><span class="line"></span><br><span class="line">                      if (registerQueue.isEmpty() &amp;&amp; cancelQueue.isEmpty()) &#123;</span><br><span class="line">                          assert acceptorRef.get() != this;</span><br><span class="line">                          break;</span><br><span class="line">                      &#125;</span><br><span class="line"></span><br><span class="line">                      if (!acceptorRef.compareAndSet(null, this)) &#123;</span><br><span class="line">                          assert acceptorRef.get() != this;</span><br><span class="line">                          break;</span><br><span class="line">                      &#125;</span><br><span class="line"></span><br><span class="line">                      assert acceptorRef.get() == this;</span><br><span class="line">                  &#125;</span><br><span class="line"></span><br><span class="line">                  if (selected &gt; 0) &#123;</span><br><span class="line">                      // We have some connection request, let&apos;s process</span><br><span class="line">                      // them here.</span><br><span class="line">                      processHandles(selectedHandles());</span><br><span class="line">                  &#125;</span><br><span class="line"></span><br><span class="line">                  // check to see if any cancellation request has been made.</span><br><span class="line">                  nHandles -= unregisterHandles();</span><br><span class="line">              &#125; catch (ClosedSelectorException cse) &#123;</span><br><span class="line">                  // If the selector has been closed, we can exit the loop</span><br><span class="line">                  ExceptionMonitor.getInstance().exceptionCaught(cse);</span><br><span class="line">                  break;</span><br><span class="line">              &#125; catch (Exception e) &#123;</span><br><span class="line">                  ExceptionMonitor.getInstance().exceptionCaught(e);</span><br><span class="line"></span><br><span class="line">                  try &#123;</span><br><span class="line">                      Thread.sleep(1000);</span><br><span class="line">                  &#125; catch (InterruptedException e1) &#123;</span><br><span class="line">                      ExceptionMonitor.getInstance().exceptionCaught(e1);</span><br><span class="line">                  &#125;</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">          // Cleanup all the processors, and shutdown the acceptor.</span><br><span class="line">          if (selectable &amp;&amp; isDisposing()) &#123;</span><br><span class="line">              selectable = false;</span><br><span class="line">              try &#123;</span><br><span class="line">                  if (createdProcessor) &#123;</span><br><span class="line">                      processor.dispose();</span><br><span class="line">                  &#125;</span><br><span class="line">              &#125; finally &#123;</span><br><span class="line">                  try &#123;</span><br><span class="line">                      synchronized (disposalLock) &#123;</span><br><span class="line">                          if (isDisposing()) &#123;</span><br><span class="line">                              destroy();</span><br><span class="line">                          &#125;</span><br><span class="line">                      &#125;</span><br><span class="line">                  &#125; catch (Exception e) &#123;</span><br><span class="line">                      ExceptionMonitor.getInstance().exceptionCaught(e);</span><br><span class="line">                  &#125; finally &#123;</span><br><span class="line">                      disposalFuture.setDone();</span><br><span class="line">                  &#125;</span><br><span class="line">              &#125;</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br></pre></td></tr></table></figure></p>
<p>如果有session连接<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">@SuppressWarnings(&quot;unchecked&quot;)</span><br><span class="line">       private void processHandles(Iterator&lt;H&gt; handles) throws Exception &#123;</span><br><span class="line">           while (handles.hasNext()) &#123;</span><br><span class="line">               H handle = handles.next();</span><br><span class="line">               handles.remove();</span><br><span class="line"></span><br><span class="line">               // Associates a new created connection to a processor,</span><br><span class="line">               // and get back a session</span><br><span class="line">               S session = accept(processor, handle);</span><br><span class="line"></span><br><span class="line">               if (session == null) &#123;</span><br><span class="line">                   continue;</span><br><span class="line">               &#125;</span><br><span class="line"></span><br><span class="line">               initSession(session, null, null);</span><br><span class="line"></span><br><span class="line">               // add the session to the SocketIoProcessor</span><br><span class="line">               session.getProcessor().add(session);</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br></pre></td></tr></table></figure></p>
<p>在<code>S session = accept(processor, handle);</code>中把之前创建的<code>SimpleIoProcessorPool</code>和session绑定,然后在<code>session.getProcessor().add(session);</code>中,<code>session.getProcessor()</code><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">@SuppressWarnings(&quot;unchecked&quot;)</span><br><span class="line">   private IoProcessor&lt;S&gt; getProcessor(S session) &#123;</span><br><span class="line">       IoProcessor&lt;S&gt; processor = (IoProcessor&lt;S&gt;) session.getAttribute(PROCESSOR);</span><br><span class="line"></span><br><span class="line">       if (processor == null) &#123;</span><br><span class="line">           if (disposed || disposing) &#123;</span><br><span class="line">               throw new IllegalStateException(&quot;A disposed processor cannot be accessed.&quot;);</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           processor = pool[Math.abs((int) session.getId()) % pool.length];</span><br><span class="line"></span><br><span class="line">           if (processor == null) &#123;</span><br><span class="line">               throw new IllegalStateException(&quot;A disposed processor cannot be accessed.&quot;);</span><br><span class="line">           &#125;</span><br><span class="line"></span><br><span class="line">           session.setAttributeIfAbsent(PROCESSOR, processor);</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       return processor;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<p>根据session的id获得一个之前在pool中放的processor,然后<code>add(session)</code><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">    * &#123;@inheritDoc&#125;</span><br><span class="line">    */</span><br><span class="line">   @Override</span><br><span class="line">   public final void add(S session) &#123;</span><br><span class="line">       if (disposed || disposing) &#123;</span><br><span class="line">           throw new IllegalStateException(&quot;Already disposed.&quot;);</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">       // Adds the session to the newSession queue and starts the worker</span><br><span class="line">       newSessions.add(session);</span><br><span class="line">       startupProcessor();</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></p>
<p>然后看<code>startupProcessor()</code>方法<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">   * Starts the inner Processor, asking the executor to pick a thread in its</span><br><span class="line">   * pool. The Runnable will be renamed</span><br><span class="line">   */</span><br><span class="line">  private void startupProcessor() &#123;</span><br><span class="line">      Processor processor = processorRef.get();</span><br><span class="line"></span><br><span class="line">      if (processor == null) &#123;</span><br><span class="line">          processor = new Processor();</span><br><span class="line"></span><br><span class="line">          if (processorRef.compareAndSet(null, processor)) &#123;</span><br><span class="line">              executor.execute(new NamePreservingRunnable(processor, threadName));</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      // Just stop the select() and start it again, so that the processor</span><br><span class="line">      // can be activated immediately.</span><br><span class="line">      wakeup();</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></p>
<p>如果这个processorRef是null还没有被初始化,就<code>processor = new Processor();</code>这个processor我们就很熟悉了<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line">     * The main loop. This is the place in charge to poll the Selector, and to</span><br><span class="line">     * process the active sessions. It&apos;s done in - handle the newly created</span><br><span class="line">     * sessions -</span><br><span class="line">     */</span><br><span class="line">    private class Processor implements Runnable &#123;</span><br><span class="line">        /**</span><br><span class="line">         * &#123;@inheritDoc&#125;</span><br><span class="line">         */</span><br><span class="line">        @Override</span><br><span class="line">        public void run() &#123;</span><br><span class="line">            assert processorRef.get() == this;</span><br><span class="line"></span><br><span class="line">            lastIdleCheckTime = System.currentTimeMillis();</span><br><span class="line">            int nbTries = 10;</span><br><span class="line"></span><br><span class="line">            for (;;) &#123;</span><br><span class="line">                try &#123;</span><br><span class="line">                    // This select has a timeout so that we can manage</span><br><span class="line">                    // idle session when we get out of the select every</span><br><span class="line">                    // second. (note : this is a hack to avoid creating</span><br><span class="line">                    // a dedicated thread).</span><br><span class="line">                    long t0 = System.currentTimeMillis();</span><br><span class="line">                    int selected = select(SELECT_TIMEOUT);</span><br><span class="line">                    long t1 = System.currentTimeMillis();</span><br><span class="line">                    long delta = t1 - t0;</span><br><span class="line"></span><br><span class="line">                    if (!wakeupCalled.getAndSet(false) &amp;&amp; (selected == 0) &amp;&amp; (delta &lt; 100)) &#123;</span><br><span class="line">                        // Last chance : the select() may have been</span><br><span class="line">                        // interrupted because we have had an closed channel.</span><br><span class="line">                        if (isBrokenConnection()) &#123;</span><br><span class="line">                            LOG.warn(&quot;Broken connection&quot;);</span><br><span class="line">                        &#125; else &#123;</span><br><span class="line">                            // Ok, we are hit by the nasty epoll</span><br><span class="line">                            // spinning.</span><br><span class="line">                            // Basically, there is a race condition</span><br><span class="line">                            // which causes a closing file descriptor not to be</span><br><span class="line">                            // considered as available as a selected channel,</span><br><span class="line">                            // but</span><br><span class="line">                            // it stopped the select. The next time we will</span><br><span class="line">                            // call select(), it will exit immediately for the</span><br><span class="line">                            // same</span><br><span class="line">                            // reason, and do so forever, consuming 100%</span><br><span class="line">                            // CPU.</span><br><span class="line">                            // We have to destroy the selector, and</span><br><span class="line">                            // register all the socket on a new one.</span><br><span class="line">                            if (nbTries == 0) &#123;</span><br><span class="line">                                LOG.warn(&quot;Create a new selector. Selected is 0, delta = &quot; + delta);</span><br><span class="line">                                registerNewSelector();</span><br><span class="line">                                nbTries = 10;</span><br><span class="line">                            &#125; else &#123;</span><br><span class="line">                                nbTries--;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125; else &#123;</span><br><span class="line">                        nbTries = 10;</span><br><span class="line">                    &#125;</span><br><span class="line">                    </span><br><span class="line">                    // Manage newly created session first</span><br><span class="line">                    if(handleNewSessions() == 0) &#123;</span><br><span class="line">                        // Get a chance to exit the infinite loop if there are no</span><br><span class="line">                        // more sessions on this Processor</span><br><span class="line">                        if (allSessionsCount() == 0) &#123;</span><br><span class="line">                            processorRef.set(null);</span><br><span class="line"></span><br><span class="line">                            if (newSessions.isEmpty() &amp;&amp; isSelectorEmpty()) &#123;</span><br><span class="line">                                // newSessions.add() precedes startupProcessor</span><br><span class="line">                                assert processorRef.get() != this;</span><br><span class="line">                                break;</span><br><span class="line">                            &#125;</span><br><span class="line"></span><br><span class="line">                            assert processorRef.get() != this;</span><br><span class="line"></span><br><span class="line">                            if (!processorRef.compareAndSet(null, this)) &#123;</span><br><span class="line">                                // startupProcessor won race, so must exit processor</span><br><span class="line">                                assert processorRef.get() != this;</span><br><span class="line">                                break;</span><br><span class="line">                            &#125;</span><br><span class="line"></span><br><span class="line">                            assert processorRef.get() == this;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    updateTrafficMask();</span><br><span class="line"></span><br><span class="line">                    // Now, if we have had some incoming or outgoing events,</span><br><span class="line">                    // deal with them</span><br><span class="line">                    if (selected &gt; 0) &#123;</span><br><span class="line">                        // LOG.debug(&quot;Processing ...&quot;); // This log hurts one of</span><br><span class="line">                        // the MDCFilter test...</span><br><span class="line">                        process();</span><br><span class="line">                    &#125;</span><br><span class="line">                    </span><br><span class="line">                    // Write the pending requests</span><br><span class="line">                    long currentTime = System.currentTimeMillis();</span><br><span class="line">                    flush(currentTime);</span><br><span class="line">                    </span><br><span class="line">                    // Last, not least, send Idle events to the idle sessions</span><br><span class="line">                    notifyIdleSessions(currentTime);</span><br><span class="line">                    </span><br><span class="line">                    // And manage removed sessions</span><br><span class="line">                    removeSessions();</span><br><span class="line">                    </span><br><span class="line">                    // Disconnect all sessions immediately if disposal has been</span><br><span class="line">                    // requested so that we exit this loop eventually.</span><br><span class="line">                    if (isDisposing()) &#123;</span><br><span class="line">                        boolean hasKeys = false;</span><br><span class="line"></span><br><span class="line">                        for (Iterator&lt;S&gt; i = allSessions(); i.hasNext();) &#123;</span><br><span class="line">                            IoSession session = i.next();</span><br><span class="line"></span><br><span class="line">                            scheduleRemove((S) session);</span><br><span class="line"></span><br><span class="line">                            if (session.isActive()) &#123;</span><br><span class="line">                                hasKeys = true;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line"></span><br><span class="line">                        wakeup();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125; catch (ClosedSelectorException cse) &#123;</span><br><span class="line">                    // If the selector has been closed, we can exit the loop</span><br><span class="line">                    // But first, dump a stack trace</span><br><span class="line">                    ExceptionMonitor.getInstance().exceptionCaught(cse);</span><br><span class="line">                    break;</span><br><span class="line">                &#125; catch (Exception e) &#123;</span><br><span class="line">                    ExceptionMonitor.getInstance().exceptionCaught(e);</span><br><span class="line"></span><br><span class="line">                    try &#123;</span><br><span class="line">                        Thread.sleep(1000);</span><br><span class="line">                    &#125; catch (InterruptedException e1) &#123;</span><br><span class="line">                        ExceptionMonitor.getInstance().exceptionCaught(e1);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            try &#123;</span><br><span class="line">                synchronized (disposalLock) &#123;</span><br><span class="line">                    if (disposing) &#123;</span><br><span class="line">                        doDispose();</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; catch (Exception e) &#123;</span><br><span class="line">                ExceptionMonitor.getInstance().exceptionCaught(e);</span><br><span class="line">            &#125; finally &#123;</span><br><span class="line">                disposalFuture.setValue(true);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure></p>
<p>所以,实际上,整个过程就是有1个Acceptor,n个Processor线程控制着这些session.每个Processor之后都会为它对应的那些session的消息执行filterChain的流程.<br><img src="http://note.youdao.com/yws/res/3166/746FEDFCF7FE440CB3D95999DEF0E461" alt="image"></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>这两个图片是什么?可以扫吗?</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatPay.png" alt="Shen 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/aliPay.png" alt="Shen 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Java/" rel="tag"># Java</a>
          
            <a href="/tags/mina/" rel="tag"># mina</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/06/14/读写锁/" rel="next" title="读写锁">
                <i class="fa fa-chevron-left"></i> 读写锁
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/06/14/帧同步与状态同步/" rel="prev" title="帧同步与状态同步">
                帧同步与状态同步 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC80MjA1OS8xODYwNg=="></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/saiki.jpeg" alt="Shen">
            
              <p class="site-author-name" itemprop="name">Shen</p>
              <p class="site-description motion-element" itemprop="description">Penn's blog</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">117</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">26</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Shen</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  












  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("Gst71Gv0w5HKejm5H7LuG0XI-gzGzoHsz", "aoFAyHFDKihGTWWhHX0p8byC");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  

  

  

  

</body>
</html>
